package com.jml.zlhq.app.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.RoundAngleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainFenXiangAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;
	private Boolean isBottom;

	public MainFenXiangAdapter(Activity activity, List<JSONObject> itemList,
			boolean isBottom) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
		this.isBottom = isBottom;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class ItemHolder {
		public RoundAngleImageView image;
		public TextView name, suju, shijian, yuedu, zan, pinglun;
		public LinearLayout bottomLay;
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		ItemHolder holder = null;
		if (convertView == null) {
			holder = new ItemHolder();
			int main_fenxiangzuanImage_item = R.layout.main_fenxiangzuan_image_item;
			convertView = LayoutInflater.from(appContext).inflate(
					main_fenxiangzuanImage_item, null);
			holder.image = (RoundAngleImageView) convertView
					.findViewById(R.id.main_fenxiang_item_image);
			holder.name = (TextView) convertView
					.findViewById(R.id.main_fenxiang_item_name);
			holder.suju = (TextView) convertView
					.findViewById(R.id.main_fenxiang_item_suju);
			holder.bottomLay = (LinearLayout) convertView
					.findViewById(R.id.main_fenxiang_bottom_lay);
			holder.bottomLay.setVisibility(View.VISIBLE);
			holder.yuedu = (TextView) convertView
					.findViewById(R.id.main_fenxiang_item_yuedu);
			holder.shijian = (TextView) convertView
					.findViewById(R.id.main_fenxiang_item_shijian);
			holder.zan = (TextView) convertView
					.findViewById(R.id.main_fenxiang_item_zan);
			holder.pinglun = (TextView) convertView
					.findViewById(R.id.main_fenxiang_item_pinglun);
			convertView.setTag(holder);
		} else {
			holder = (ItemHolder) convertView.getTag();
		}
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			JSONObject json = itemList.get(position);
			Log.e("aa", URLs.IMAGE_HOST + BaseTools.getFrom("image.image_url", json)+ "_scal_360x360.jpg");
			ImageLoader.getInstance().displayImage(
							URLs.IMAGE_HOST
									+ BaseTools.getFrom("image.image_url", json)+ "_scal_360x360.jpg",
							holder.image,
							UIUtil.getImageLoaderDisplayOptions(R.drawable.icon_default_z));
			holder.name.setText(BaseTools.getFrom("title", json));
			holder.yuedu.setText(Html.fromHtml(BaseTools.getFrom("stats.vistors", json) + "人浏览"));
			holder.zan.setText(Html.fromHtml(BaseTools.getFrom("stats.ups", json) + "赞"));
			holder.pinglun.setText(Html.fromHtml(BaseTools.getFrom("stats.comments", json) + "评论"));
//			holder.ising.setVisibility(BaseTools.getFrom("is_expire", json,
//					"false").equals("false") ? View.GONE : View.VISIBLE);
//			holder.nomore.setVisibility(position == itemList.size() - 1
//			&& pageEnd ? View.VISIBLE : View.GONE);
//			holder.suju.setText("时间："+BaseTools.getFrom("create_at", json));
			
			if(BaseTools.getFrom("type", json).equals("懂段子")||BaseTools.getFrom("type", json).equals("涨姿势")){
				Log.e("aa2222222", BaseTools.getFrom("content_html", json));
				holder.suju.setText(Html.fromHtml(BaseTools.getFrom("content_html", json)));
			}
			boolean ising = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

}
